<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\product\controller;

use  Mscore\Core\Controller;
use  app\product\model\Protrade as ProtradeModel;
use  app\product\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class ProtradeController extends Controller{

    private $model;
    private $login;
    private $error;
    private $success;
    private $language;
    
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new ProtradeModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 运营列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){  
        //文章类
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1 ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                if(post('procode')){
                    $where .= ' and a.procode like "%'.post('procode').'%"';
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data = [
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $this->display('list.html');
                break;
        }
    }

    /**
     * 添加
     * @param array $add 
     * @param array $where 
     */ 
    public function add(){
        $params = post();
        switch (true) {
            case $params:
                $data = $this->model->save($params);
                switch ($data) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                } 
                break;
            default:
                if(get('id')){
                    $datainfo =$this->model->info(get('id'));
                }
                $pro = (new CommonModel())->getTrade('dels<1 and status<2','id,title,procode,cid,cname');
                //定义变量datainfo输出
                $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:null;
                $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:null;
                $datainfo['proid']=isset($datainfo['proid'])?$datainfo['proid']:0;
                $datainfo['procode']=isset($datainfo['procode'])?$datainfo['procode']:null;
                $datainfo['cid']=isset($datainfo['cid'])?$datainfo['cid']:0;
                $datainfo['cname']=isset($datainfo['cname'])?$datainfo['cname']:null;
                $datainfo['sentiment']=isset($datainfo['sentiment'])?$datainfo['sentiment']:array('bull'=>'50%','bear'=>'50%');
                $datainfo['contract']=isset($datainfo['contract'])?$datainfo['contract']:array('lmin'=>10,'spre'=>40,'size'=>10,'max'=>10,'min'=>10);
                $datainfo['forecast']=isset($datainfo['forecast'])?$datainfo['forecast']:array(['dayn'=>'1周','mean'=>1,'goup'=>35,'drop'=>40,'plate'=>25,'trend'=>2]);
                
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:2;

                $this->assign('datainfo',$datainfo);
                $this->assign('grade',$this->login['info']['grade']);
                $this->assign('pro',$pro);
                $this->display('add.html');
                break;
        }
    }
   
}
